﻿using System;
using System.Collections.Generic;
using System.Text;
using SoccerEvaluator.Common;

namespace SoccerEvaluator
{
    /// <summary>
    /// 
    /// </summary>
    public class Evaluator
    {
        /// <summary>
        /// 
        /// </summary>
        private int _pointExactScore;
        /// <summary>
        /// 
        /// </summary>
        private int _pointResultScore;

        /// <summary>
        /// 
        /// </summary>
        private string _exactScoreColor;
        /// <summary>
        /// 
        /// </summary>
        private string _resultScoreColor;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="exactScore"></param>
        /// <param name="resultScore"></param>
        public Evaluator(int exactScore, string exactScoreColor, int resultScore, string resultScoreColor)
        {
            _pointExactScore = exactScore;
            _pointResultScore = resultScore;

            _resultScoreColor = resultScoreColor;
            _exactScoreColor = exactScoreColor;

        }
        /// <summary>
        /// Evalua el puntaje de un usuario en base a un template especificado
        /// </summary>
        /// <param name="index"></param>
        /// <param name="FIFAResult"></param>
        /// <param name="userResult"></param>
        /// <returns></returns>
        public int Evaluate(TemplateInfo FIFAResult, TemplateInfo userResult)
        {
            userResult.Total = userResult.ExactScore = userResult.CatchScore = 0;

            if (userResult.Scores != null)
            {
                for (int i = 0; i < FIFAResult.Scores.Count; i++)
                {
                    try
                    {
                        TemplateScoreInfo fifa = FIFAResult.Scores[i];

                        TimeSpan ts = DateTime.Now - fifa.Date;

                        //Solo asigna puntos a los partidos habilitados
                        if (ts.TotalMilliseconds > 0)
                        {
                            TemplateScoreInfo user = userResult.Scores[i];

                            //coinciden los goles
                            if (user.LocalGoals == fifa.LocalGoals && user.VisitorGoals == fifa.VisitorGoals)
                            {
                                userResult.ExactScore += 1;

                                user.Color = _exactScoreColor;
                            }
                            else
                            {
                                //si coincide el marcador 

                                //Empate
                                if (user.LocalGoals == user.VisitorGoals && fifa.LocalGoals == fifa.VisitorGoals)
                                {
                                    userResult.CatchScore += 1;

                                    user.Color = _resultScoreColor;
                                }
                                else if (user.LocalGoals > user.VisitorGoals && fifa.LocalGoals > fifa.VisitorGoals)
                                {
                                    //gano el local
                                    userResult.CatchScore += 1;
                                    user.Color = _resultScoreColor;
                                }
                                else if (user.LocalGoals < user.VisitorGoals && fifa.LocalGoals < fifa.VisitorGoals)
                                {
                                    //perdio el local
                                    userResult.CatchScore += 1;
                                    user.Color = _resultScoreColor;
                                }
                            }
                        }
                    }
                    catch
                    {

                    }
                }

            }

            //Puntaje total por el partido
            userResult.Total = _pointExactScore * userResult.ExactScore + _pointResultScore * userResult.CatchScore;

            return userResult.Total;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="fifa"></param>
        /// <param name="rowIndex"></param>
        /// <param name="scores"></param>
        /// <param name="results"></param>
        /// <param name="userResults"></param>
        public void GetSummary(TemplateScoreInfo fifa, int rowIndex, out List<string> scores, out List<string> results, out List<string> others, List<TemplateInfo> userResults)
        {
            scores = new List<string>();
            results = new List<string>();
            others = new List<string>();


            foreach (TemplateInfo result in userResults)
            {
                System.Diagnostics.Trace.WriteLine(result.Name);

                TemplateScoreInfo user = result.Scores[rowIndex];

                //Verificar resultado

                //Si agarraron todos los goles
                if (user.LocalGoals == fifa.LocalGoals && user.VisitorGoals == fifa.VisitorGoals)
                {
                    scores.Add(result.Name);
                }
                else
                {
                    string userScore = string.Format("{0}-{1}  {2}", user.LocalGoals, user.VisitorGoals, result.Name);

                    //Empate
                    if (user.LocalGoals == user.VisitorGoals && fifa.LocalGoals == fifa.VisitorGoals)
                    {
                        results.Add(userScore);
                    }
                    else if (user.LocalGoals > user.VisitorGoals && fifa.LocalGoals > fifa.VisitorGoals)
                    {
                        //gano el local
                        results.Add(userScore);
                    }
                    else if (user.LocalGoals < user.VisitorGoals && fifa.LocalGoals < fifa.VisitorGoals)
                    {
                        //perdio el local
                        results.Add(userScore);

                    }
                    else
                    {
                        others.Add(userScore);
                    }
                }


                scores.Sort();
                results.Sort();
                others.Sort();
            }
        }
    }
}
